//
//  TTZFBPayTool.m
//  TaoTuo
//
//  Created by admin on 2019/11/13.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTZFBPayTool.h"
#import <AlipaySDK/AlipaySDK.h>

#define appScheme @"taotuo"

@implementation TTZFBPayTool

/// 开通商城支付支付
+ (void)gotoOpenPlusPayWithMoney:(NSString *)money year:(NSString *)year isRenewal:(BOOL)renewal notifyKey:(nonnull NSString *)notifyKey
{
    
    TTLoginSingle *single = [TTLoginSingle sharedSingle];
    
    NSString *title = renewal ? @"个人店铺续费" : @"个人店铺";
    NSString *subTitle = renewal ? @"个人店铺续费" : @"APP在线开通个人店铺服务";
    
    TTZFBPayModel *model = [[TTZFBPayModel alloc] init];
    model.paymentBusines = renewal ? @"3" : @"1";
    model.platformUserAccount = single.loginModel.Mobile;
    model.platformUserName = single.loginModel.Name;
    model.payAmount = money;
    model.paymentType = @"2";
    model.paymentMethod = @"1";
    model.year = year;
    model.goodsTitle = title;
    model.goodsTitleSub = subTitle;
    NSDictionary *dic = [model modelToDic];
    ///创建订单
    [self createOrderNumberWithParams:dic notifyKey:notifyKey];
    
    
}

///创建订单
+ (void)createOrderNumberWithParams:(NSDictionary *)dic notifyKey:(nonnull NSString *)notifyKey
{
    // 保存通知的key
    [TTLoginSingle sharedSingle].alipaySuccessNotifyKey = notifyKey;
    
    UIView *view = [UIApplication sharedApplication].keyWindow;
    [MBProgressHUD showLoadingAnimationOnView:view];
    
    [[LLNetworkManagerTool sharedManager] postUrlString:alipay_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSString *orderSerialNumber = responseObject[@"OrderSerialNumber"];
        // 获取订单签名
        [self getOrderSignWithOrderSerialNumber:orderSerialNumber];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        [MBProgressHUD dismissAnimationOnView:view];
        
    } showLoadingInView:nil];
}

// 获取订单签名
+ (void)getOrderSignWithOrderSerialNumber:(NSString *)orderSerialNumber
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:@"https://alipay.idecd.cn/AppAlipay.aspx" parameters:@{@"OrderSerialNumber" : orderSerialNumber} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [MBProgressHUD dismissAnimationOnView:view];
        // 签名
        
        [[AlipaySDK defaultService] payOrder:responseObject fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            if ([resultDic[@"resultStatus"] intValue] == 9000) {
                // 支付成功
//                [[NSNotificationCenter defaultCenter] postNotificationName:openDianpuSuccessNotifyKey object:nil];
            } else {
                NSString *string = [NSString stringWithFormat:@"%@ 支付失败", resultDic[@"resultStatus"]];
                [view makeToast:string];
            }
        }];
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        [MBProgressHUD dismissAnimationOnView:view];
    } showLoadingInView:nil];
}





@end
